package project.tool4nat.common;

import io.netty.channel.Channel;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.atomic.AtomicLong;

public class ChannelManager {

    private static volatile Channel proxyTunnel;

    private static Map<Long, Channel> callerMap = new ConcurrentHashMap<>();

    private static AtomicLong callerIdSeq = new AtomicLong(0);

    public static void setProxyTunnel(Channel proxyTunnel) {
        ChannelManager.proxyTunnel = proxyTunnel;
    }

    public static Channel getProxyTunnel() {
        return proxyTunnel;
    }

    public static void addCaller(Long cid, Channel channel) {
        callerMap.put(cid, channel);
    }

    public static void removeCaller(Long cid) {
        callerMap.remove(cid);
    }

    public static Channel getCaller(Long cid) {
        return callerMap.get(cid);
    }

    public static Long newCid() {
        return callerIdSeq.getAndIncrement();
    }

    private volatile static Set<Channel> channelSet = new ConcurrentSkipListSet<>();

    public static void add(Channel channel) {
        channelSet.add(channel);
    }

    public static int getSize() {
        return channelSet.size();
    }
}
